package jp.ac.u_tokyo.dch.util.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.xwork.StringUtils;

import jp.ac.u_tokyo.dch.model.ModoruBean;
/**
 * @version 1.1
 */
public class ModoruUtil {

	/**
	 * キー
	 */
	private static final String KEY = "MODORU_OBJ";

	/**
	 * 戻る先をスタックに保存する
	 * @param request リクエスト
	 * @param modoru 戻る先
	 * @param param パラメタ
	 */
	public static void modoruProcess(HttpServletRequest request, String modoru, String param) {

		HttpSession session = request.getSession(true);
		ModoruBean bean = (ModoruBean) session.getAttribute(KEY);

		if (bean == null) {
			bean = new ModoruBean();
		}
		if (StringUtils.isEmpty(param)) {
			param = "";
		}
		bean.push(modoru, param);

		session.setAttribute(KEY, bean);
	}

	/**
	 * 削除する
	 * @param request リクエスト
	 */
	public static void clear(HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		ModoruBean bean = (ModoruBean) session.getAttribute(KEY);
		if (bean != null) {
			bean.clear();
		}
	}

	/**
	 * 戻る先を取得する
	 * @param request リクエスト
	 * @return 戻る先
	 */
	public static String modoruUrl(HttpServletRequest request) {

		HttpSession session = request.getSession(true);
		ModoruBean bean = (ModoruBean) session.getAttribute(KEY);

		if (bean == null) {
			return "/main";
		}

		return bean.modoruUrl();
	}

}
